/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.commons.web.jsf.utils;


import javax.activation.MimetypesFileTypeMap;
import javax.faces.event.ActionEvent;

/**
 * @author Jorge E. Villaverde
 * @version 0.0.1
 * @since 0.0.1
 */
public class FacesUtil {
	public static final String XLS_FILE_TYPE = "xls";

	public static final String CSV_FILE_TYPE = "csv";

	public static final String PDF_MIME_TYPE = "application/pdf";
	
	public static final String MS_WORD_MIME_TYPE = "application/msword";

	public static final String MS_EXCEL_MIME_TYPE = "application/vnd.ms-excel";
	
	public static final String MS_POWER_POINT_MIME_TYPE = "application/vnd.ms-powerpoint";

	public static final String MS_WORD_2007_MIME_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

	public static final String MS_EXCEL_2007_MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
	
	public static String getActionAttribute(ActionEvent event, String name) {
		return (String) event.getComponent().getAttributes().get(name);
	}
	
	public static String guessMimeType(String fileName){
		String mime = "application/octet-stream";
		
		fileName = fileName.toLowerCase();
        int extDot = fileName.lastIndexOf('.');
        if (extDot > 0) {
            String extension = fileName.substring(extDot + 1);
            if ("bmp".equals(extension)) {
                mime = "image/bmp";
            } else if ("jpg".equals(extension)) {
                mime = "image/jpeg";
            } else if ("gif".equals(extension)) {
                mime = "image/gif";
            } else if ("png".equals(extension)) {
                mime = "image/png";
            } else if ("pdf".equals(extension)) {
            	mime = PDF_MIME_TYPE;
            } else if ("doc".equals(extension)){
            	mime = MS_WORD_MIME_TYPE;
            } else if ("xls".equals(extension)){
            	mime = MS_EXCEL_MIME_TYPE;
            } else if ("ppt".equals(extension)){
            	mime = MS_POWER_POINT_MIME_TYPE; 
            } else if ("docx".equals(extension)) {
            	mime = MS_WORD_2007_MIME_TYPE;
            } else if ("xlsx".equals(extension)) {
            	mime = MS_EXCEL_2007_MIME_TYPE;
            } else if ("pptx".equals(extension)) {
            	mime = "application/vnd.openxmlformats-officedocument.presentationml.presentation";            	
            } else if ("zip".equals(extension)) {
            	mime = "application/zip";
            } else if ("rar".equals(extension)) {
            	mime = "application/x-rar-compressed";
            } else {
            	MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
            	// only by file name
            	String mimeType = mimeTypesMap.getContentType(fileName);
                mime = mimeType;
            }
        }
        return mime;
	}
	
	public static String humanReadableByteCount(long bytes, boolean si) {
	    int unit = si ? 1000 : 1024;
	    if (bytes < unit) return bytes + " B";
	    int exp = (int) (Math.log(bytes) / Math.log(unit));
	    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
	    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
	}	
	
}
